<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>别踩白块小游戏</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ::-webkit-scrollbar{
            display: none !important;
        }
        body,html{
            width: 100%;
            height: 100%;
            background-color: black;
            user-select: none;
        }
        .box{
            /* width: 400px; 
            height: 600px; */
            width: 100%;
            height: 90%;
            background-color: white;
            margin: 0px auto;
            position: relative;
            overflow: hidden;
        }
        .start{
            position: absolute;
            top: 0;
            left: 0;
            /* width: 400px;
            height: 70px; */
            width: 100%;
            height: 10%;
            background-color: #fff;
            /* font-size: 50px; */
            font-size: 2rem;
            font-weight: bold;
            text-align: center;
            /* line-height: 70px; */
            line-height: 2;
            border-bottom: 1px solid;
            cursor: pointer;
            user-select: none;
            z-index: 99;
        }
        .gameStatus{
            /* width: 400px;
            height: 50px; */
            width: 100%;
            height: 10%;
            margin: 0 auto;
            background-color: #fff;
            text-align: center;
            /* line-height: 50px; */
            line-height: 2;
            /* font-size: 30px; */
            font-size: 2rem;
            font-weight: bold;
            border-top: 1px solid;
        }
        .main{
            /* width: 400px;
            height: 600px; */
            width: 100%;
            height: 100%;
            position: relative;
            /* top: -150px; */
            top: -25%;
        }
        .fenshu{
            /* width: 400px;
            height: 600px; */
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            background-color: rgba(0, 0, 0, 0.8);
            text-align: center;
            font-weight: bolder;
            /* font-size: 40px; */
            font-size: 3rem;
            display: none;
            color: skyblue;
        }
        .fenshu>div{
            width: 100%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
        /* .fenshu>div:first-child{ */
            /* margin-top: 250px; */
            /* margin-top: 41%; */
            /* top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
        .fenshu p:last-child{
            top: 52%;
            left: 50%;
            transform: translate(-50%,-50%);
        } */
        .row{
            /* width: 400px;
            height: 150px; */
            width: 100%;
            height: 25%;
        }
        .col{
            /* width: 100px;
            height: 150px; */
            width: 25%;
            height: 100%;
            float: left;
            border: 1px solid;
            border-top: none;
            border-left: none;
            box-sizing: border-box;
        }
        audio{
            height: 0;
        }
    </style>
    <script src="tools.js"></script>
</head>
<body>
    <div class="box">
        <!-- 开始按钮 -->
        <div class="start">开始游戏</div>
        <!-- 游戏主体部分 -->
        <div class="main"></div>
        <div class="fenshu"></div>
        <audio src="./music/想去海边.mp3" preload="auto" controls="controls" loop autoplay  id="mymusic"></audio>
    </div>
    <!-- 游戏状态 -->
    <div class="gameStatus">游戏结束</div>
    <script>
        window.onload=function(){
            var box=getClass("box")[0];
            var start=getClass("start")[0];
            var main=getClass("main")[0];
            var gameStatus=getClass("gameStatus")[0];
            var fenshu=getClass("fenshu")[0];
            var timer=null;
            var num=0;
            var step=5;
            start.onclick=function(){
                main.innerHTML="";
                step=5;
                num=0;
                fenshu.style.display="none";
                gameStatus.innerText="游戏开始";
                start.style.display="none";
                cDiv();
                timer=setInterval(function(){
                    main.style.top=main.offsetTop+step+"px";
                    console.log(step);
                    if(main.offsetTop>=box.offsetTop){
                        main.style.top=-25+"%";
                        cDiv();
                    }
                    if(main.children.length==6){
                        for(var i=0;i<4;i++){
                            if(main.children[main.children.length-1].children[i].key==1){
                                clearInterval(timer);
                                start.style.display="block";
                                start.innerText="重新开始";
                                gameStatus.innerText="游戏结束";
                                fenshu.style.display="block";
                                fenshu.innerHTML="<div><p>游戏结束<p><p>"+num+"分<p></div>";
                            }
                        }
                        main.removeChild(main.children[main.children.length-1]);
                    }
                },30);
            }
            function cDiv(){
                var rowDiv=document.createElement("div");
                rowDiv.className="row";
                var index=parseInt(Math.random()*4);
                for(var i=0;i<4;i++){
                    var colDiv=document.createElement("div");
                    colDiv.className="col";
                    if(i==index){
                        colDiv.style.backgroundColor="black";
                        colDiv.key=1;
                    }
                    colDiv.onclick=function(){
                        if(this.key==1){
                            this.style.backgroundColor="#ccc";
                            this.key=0;
                            num++;
                            if(num%5==0 && num!=0){
                                step++;
                            }
                        }else{
                            clearInterval(timer);
                            start.style.display="block";
                            start.innerText="重新开始";
                            gameStatus.innerText="游戏结束";
                            fenshu.style.display="block";
                            fenshu.innerHTML="<div><p>游戏结束<p><p>"+num+"分<p></div>";
                        }
                    }
                    rowDiv.appendChild(colDiv);
                }
                if(main.children.length==0){
                    main.appendChild(rowDiv);
                }else{
                    main.insertBefore(rowDiv,main.children[0]);
                }
            }
        }
    </script>
</body>
</html>